home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / Open Prolog / External Predicates… / Sources / SoundPlay.p < prev   
Text File  |  1993-04-15  |  8KB  |  302 lines

  1. {$D+} { MacsBug symbols on }
  2. {$R-} { No range checking }
  3.  
  4. UNIT prlxsample;
  5.  
  6.   INTERFACE
  7.  
  8.     USES memtypes, quickdraw, osintf, toolintf, packintf, prlxdefinitions,prlxLibraries;
  9.  
  10.     PROCEDURE entrypoint(plist: prlxptr);
  11.  
  12.   IMPLEMENTATION
  13.  
  14.     PROCEDURE main(plist: prlxptr);
  15.       FORWARD;
  16.  
  17.     PROCEDURE entrypoint(plist: prlxptr);
  18.  
  19.       BEGIN
  20.         main(plist);
  21.       END;
  22.  
  23.     PROCEDURE main;
  24.  
  25.       VAR
  26.         s: str255;
  27.         i: integer;
  28.         l, m: longint;
  29.  
  30.       PROCEDURE macsbug(VAR st: str255);
  31.         INLINE $ABFF;
  32.  
  33.       PROCEDURE writestr(st: str255);
  34.  
  35.         BEGIN
  36.           WITH plist^ DO
  37.             BEGIN
  38.             callbackrequest := writestring;
  39.             s := st;
  40.             callback(entrypoint);
  41.             END;
  42.         END;
  43.  
  44.       PROCEDURE writelnstr(st: str255);
  45.  
  46.         BEGIN
  47.           WITH plist^ DO
  48.             BEGIN
  49.             callbackrequest := writelnstring;
  50.             s := st;
  51.             callback(entrypoint);
  52.             END;
  53.         END;
  54.  
  55.       PROCEDURE errorstr(st: str255);
  56.  
  57.         BEGIN
  58.           WITH plist^ DO
  59.             BEGIN
  60.             callbackrequest := writeerror;
  61.             s := st;
  62.             callback(entrypoint);
  63.             END;
  64.         END;
  65.  
  66.       FUNCTION returnValue(termNumber: termIndex;
  67.                            n: longint): boolean;
  68.  
  69.         BEGIN
  70.           WITH plist^ DO
  71.             BEGIN
  72.             callbackrequest := unifyToInteger;
  73.             callbackdata[1] := termnumber;
  74.             callbackData[2] := n;
  75.             callback(entrypoint);
  76.             returnValue := callbackData[3] = messageOK;
  77.             END;
  78.         END;
  79.  
  80.       FUNCTION returnStructure(termNumber: termIndex;
  81.                                st: str255;
  82.                                arity: integer): boolean;
  83.  
  84.         BEGIN
  85.           WITH plist^ DO
  86.             BEGIN
  87.             callbackrequest := unifyToFunctor;
  88.             callbackdata[1] := termnumber;
  89.             callbackData[3] := arity;
  90.             s := st;
  91.             callback(entrypoint);
  92.             returnStructure := callbackData[3] = messageOK;
  93.             END;
  94.         END;
  95.  
  96.       FUNCTION returnAtom(termNumber: termIndex;
  97.                           st: str255): boolean;
  98.  
  99.         BEGIN
  100.           returnAtom := returnStructure(termNumber, st, 0);
  101.         END;
  102.  
  103.       FUNCTION subterm(subtermordinate: integer;
  104.                        termnumber: termindex): termindex;
  105.  
  106.         BEGIN
  107.           WITH plist^ DO
  108.             BEGIN
  109.             callbackrequest := getsubterm;
  110.             callbackdata[1] := termnumber;
  111.             callbackdata[2] := subtermordinate;
  112.             callback(entrypoint);
  113.             subterm := callbackdata[3];
  114.             END;
  115.         END;
  116.  
  117.       FUNCTION number(termnumber: termindex): boolean;
  118.  
  119.         BEGIN
  120.           WITH plist^ DO
  121.             BEGIN
  122.             callbackrequest := getterminfo;
  123.             callbackdata[1] := termnumber;
  124.             callback(entrypoint);
  125.             number := (callbackdata[1] = integertag);
  126.             END;
  127.         END;
  128.  
  129.       FUNCTION atom(termnumber: termindex): boolean;
  130.  
  131.         BEGIN
  132.           WITH plist^ DO
  133.             BEGIN
  134.             callbackrequest := getterminfo;
  135.             callbackdata[1] := termnumber;
  136.             callback(entrypoint);
  137.             atom := (callbackdata[1] = atomtag);
  138.             END;
  139.         END;
  140.  
  141.       FUNCTION structure(termnumber: termindex): boolean;
  142.  
  143.         BEGIN
  144.           WITH plist^ DO
  145.             BEGIN
  146.             callbackrequest := getterminfo;
  147.             callbackdata[1] := termnumber;
  148.             callback(entrypoint);
  149.             structure := (callbackdata[1] = structuretag);
  150.             END;
  151.         END;
  152.  
  153.       FUNCTION variable(termnumber: termindex): boolean;
  154.  
  155.         BEGIN
  156.           WITH plist^ DO
  157.             BEGIN
  158.             callbackrequest := getterminfo;
  159.             callbackdata[1] := termnumber;
  160.             callback(entrypoint);
  161.             variable := (callbackdata[1] = variabletag);
  162.             END;
  163.         END;
  164.  
  165.       FUNCTION value(termnumber: termindex): longint;
  166.  
  167.         BEGIN
  168.           WITH plist^ DO
  169.             BEGIN
  170.             callbackrequest := getterminfo;
  171.             callbackdata[1] := termnumber;
  172.             callback(entrypoint);
  173.             IF callbackdata[1] = integertag THEN
  174.               value := callbackdata[2]
  175.             ELSE
  176.               errorstr('attempt to get value of a non-integer');
  177.             END;
  178.         END;
  179.  
  180.       FUNCTION arity(termnumber: termindex): integer;
  181.  
  182.         BEGIN
  183.           WITH plist^ DO
  184.             BEGIN
  185.             callbackrequest := getterminfo;
  186.             callbackdata[1] := termnumber;
  187.             callback(entrypoint);
  188.             CASE callbackdata[1] OF
  189.               atomtag, integertag, variabletag: arity := 0;
  190.               structuretag: arity := callbackdata[2];
  191.               OTHERWISE errorstr('Funny data from getTermInfo in arity');
  192.             END;
  193.             END;
  194.         END;
  195.  
  196.       FUNCTION text(termnumber: termindex): str255;
  197.  
  198.         VAR
  199.           st: str255;
  200.           i: integer;
  201.  
  202.         BEGIN
  203.           WITH plist^ DO
  204.             BEGIN
  205.             callbackrequest := getterminfo;
  206.             callbackdata[1] := termnumber;
  207.             callback(entrypoint);
  208.             CASE callbackdata[1] OF
  209.               atomtag, structuretag: text := s;
  210.               integertag:
  211.                 BEGIN
  212.                 numtostring(callbackdata[2], st);
  213.                 text := st;
  214.                 END;
  215.               variabletag:
  216.                 BEGIN
  217.                 numtostring(callbackdata[2], st);
  218.                 FOR i := 255 DOWNTO 2 DO st[i] := st[i - 1];
  219.                 st[1] := '_';
  220.                 text := st;
  221.                 END;
  222.               OTHERWISE errorstr('Funny data from getTermInfo in text');
  223.             END;
  224.             END;
  225.         END;
  226.  
  227.       PROCEDURE play;
  228.  
  229.         VAR
  230.           result: osErr;
  231.           theChannel: sndChannelPtr;
  232.           theSnd: handle;
  233.           level: integer;
  234.           total, contig: longint;
  235.           soundName: str255;
  236.  
  237.         BEGIN
  238.           plist^.successful := false;
  239.           plist^.determinate := true;
  240.           theChannel := NIL;
  241.           soundName := text(1); {do this before purgeSpace to ensure runtime
  242.                                  stuff is included }
  243.           purgeSpace(total, contig);
  244.           setResLoad(false);
  245.           theSnd := getnamedResource('snd ', soundName);
  246.           setResLoad(true);
  247.           IF resError = noErr THEN
  248.             IF sizeResource(theSnd) + 2 * 1024 < contig THEN
  249.               BEGIN
  250.               getSoundVol(level);
  251.               IF value(2) <> 0 THEN setSoundVol(value(2));
  252.               loadResource(theSnd);
  253.               hNoPurge(theSnd);
  254.               IF resError = noErr THEN
  255.                 plist^.successful := (sndPlay(NIL, theSnd, true) = noErr);
  256.               hPurge(theSnd); {don't dispose of it - you might use it again!}
  257.               setSoundVol(level);
  258.               END;
  259.  
  260.         END; { procedure }
  261.  
  262.       BEGIN
  263.         WITH plist^ DO
  264.           BEGIN
  265.           CASE request OF
  266.             getPRLXInfo: 
  267.                         begin
  268.                         data[1] := 1; {number of predicates defined}
  269.                         data[2]:=eventsVersion;
  270.                         end;
  271.             initialisepredicate:
  272.               CASE id OF
  273.                 1: {play/2}
  274.                   BEGIN
  275.                   s := 'play'; {name}
  276.                   data[1] := 2; {arity}
  277.                   data[2] := 0; {permanent data}
  278.                   END;
  279.                 OTHERWISE
  280.                   errorstr('predicate index out of range at initialise');
  281.               END;
  282.             callpredicate:
  283.               BEGIN
  284.               successful := true;
  285.               CASE id OF
  286.                 1: play;
  287.                 OTHERWISE errorstr('predicate index out of range at call');
  288.               END;
  289.               END;
  290.             closepredicate:
  291.               BEGIN
  292.               CASE id OF
  293.                 1: {play} ;
  294.                 OTHERWISE errorstr('predicate index out of range at close');
  295.               END;
  296.               END;
  297.             OTHERWISE errorstr('unknown call to external procedures');
  298.           END;
  299.           END;
  300.       END;
  301. END.
  302.